<script setup>
import { ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import emitter from '@/utils/mitt.js'

const props = defineProps(['userTravelsList'])

const left = ref([])
const right = ref([])
const route = useRoute()
const router = useRouter()

const computeRenderHeight = (realHeight, realWidth) => {
  const rootFontSize = parseFloat(window.getComputedStyle(document.documentElement).fontSize)
  const screenWidth = document.documentElement.offsetWidth
  const renderWidth = (screenWidth - 0.26667 * 3 * rootFontSize) / 2
  const renderHeight = renderWidth / realWidth * realHeight / rootFontSize
  return renderHeight
}

const toDetailView = (id) => {
  if (route.path !== '/details-page') {
    router.push(`/details-page?article_id=${id}`)
  }
  else {
    router.replace(`/details-page?article_id=${id}`)
    emitter.emit('toAnotherDetailView', { id })
  }
}

watch(() => {
  return props.userTravelsList
}, (newValue) => {
  left.value = []
  right.value = []
  let lth = 0
  let rth = 0
  for (let i = 0; i < newValue.length; i++) {
    const renderHeight = computeRenderHeight(newValue[i].cover_image.height, newValue[i].cover_image.width)
    if (lth <= rth) {
      left.value.push(newValue[i])
      lth = lth + renderHeight
    }
    else {
      right.value.push(newValue[i])
      rth = rth + renderHeight
    }
  }
})
</script>

<template>
  <div class="container">
    <div class="left-column">
      <div class="item" @click="toDetailView(item._id)" v-for="item in left" :key="item._id">
        <div style="position: relative; display: flex;">
          <van-image :height="`${computeRenderHeight(item.cover_image.height, item.cover_image.width)}rem`"
            radius="0.26rem" fit="cover" :src="item.cover_image.url" />
          <div class="position">
            <van-icon size="0.4rem" color="white" name="location" />
            <span class="nowrap-ellipsis">{{ item.address }}</span>
          </div>
        </div>
        <van-text-ellipsis class="title" :content="item.title" />
        <div class="info">
          <div class="left">
            <van-image round fit="cover" :src="item.author_data[0].avatarUrl" />
            <span class="username nowrap-ellipsis">{{ item.author_data[0].nickname }}</span>
          </div>
          <div class="right">
            <van-icon size="0.45rem" name="like-o" />
            <span>{{ item.likes }}</span>
          </div>
        </div>
      </div>
    </div>
    <div class="right-column">
      <div class="item" @click="toDetailView(item._id)" v-for="item in right" :key="item._id">
        <div style="position: relative; display: flex;">
          <van-image :height="`${computeRenderHeight(item.cover_image.height, item.cover_image.width)}rem`"
            radius="0.26rem" fit="cover" :src="item.cover_image.url" />
          <div class="position">
            <van-icon size="0.4rem" color="white" name="location" />
            <span class="nowrap-ellipsis">{{ item.address }}</span>
          </div>
        </div>
        <van-text-ellipsis class="title" :content="item.title" />
        <div class="info">
          <div class="left">
            <van-image round fit="cover" :src="item.author_data[0].avatarUrl" />
            <span class="username nowrap-ellipsis">{{ item.author_data[0].nickname }}</span>
          </div>
          <div class="right">
            <van-icon size="0.45rem" name="like-o" />
            <span>{{ item.likes }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.container {
  display: flex;
  padding: 10px;

  .left-column {
    display: flex;
    flex-direction: column;
    flex: 1;
    margin-right: 10px;
  }

  .right-column {
    display: flex;
    flex-direction: column;
    flex: 1;
  }

  .item {
    margin-bottom: 20px;

    .van-image {
      width: 100%;
    }

    .position {
      position: absolute;
      bottom: 0;
      left: 0;
      right: 0;
      display: flex;
      align-items: center;
      padding: 10px;

      span {
        flex: 1;
        margin-left: 5px;
        color: white;
        font-size: 12px;
        font-weight: bold;
      }
    }

    .title {
      margin: 8px 0;
      font-weight: bold;
      font-size: 14px;
    }

    .info {
      display: flex;
      align-items: center;
      justify-content: space-between;

      .left {
        display: flex;
        align-items: center;

        .van-image {
          width: 20px;
          height: 20px;
        }

        .username {
          margin-left: 5px;
          width: 100px;
          font-size: 12px;
        }
      }

      .right {
        display: flex;
        align-items: center;

        span {
          margin-left: 5px;
          font-size: 14px;
        }
      }
    }
  }
}
</style>
